// pages/my/edit-user-info/edit-user-info.js
import WxValidate from '../../../utils/WxValidate';
import { areaList } from '@vant/area-data';
const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

  /**
   * 页面的初始数据
   */
  data: {
// 地址选择
    area:'',
    areaList,//省市区
    showarea:false,
    form: {
      id: '',
      ucode: '',
      logo: '',
      logo1: '',
      name: '',
      address: '',
      date: '',
      capital: '',
      category: '',
      pic: '',
      pic1: '',
      introduction: '',
      address_detail:''
      
    },
    show1:false,
    value02:'',
    category:[],
    disabled: ''

  },
 // 选择省市区
 showAreawin(){
   console.log(431343)
  this.setData({
    showarea: true,
  });
},
onClose(){
  this.setData({
    showarea: false,
  });
},
confirm (e) {
  let area = '';
  e.detail.values.map(item=>{
    area=area + item.name + ' '
  })
  let { form } = this.data;
  form.address = area;
  this.setData({
    area,
    showarea: false,
  });
  // 需要手动对 checked 状态进行更新
  // this.setData({ checked1: detail });
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userinfo = app.globalData.userinfo;

    let that = this;

    if(!userinfo){
       wx.redirectTo({
         url: '/pages/login/login',
       })
    }

    let param = {
         ucode : app.globalData.userinfo.ucode,
    }
    let form = this.data.form
    api.get_enterprise(param).then(function(res){
         if(res.data.hasOwnProperty('id')){
            form.id = res.data.id;
            if(res.data.logo){
              form.logo = app.globalData.siteUrl + res.data.logo;
            }else{
              form.logo = '';
            }
            
            form.logo1 = res.data.logo;
            form.name = res.data.name;
            form.address = res.data.address;
            form.address_detail = res.data.address_detail;
            form.date = res.data.date;
            form.capital = res.data.capital;
            form.category = res.data.category;
            if(res.data.pic){
              form.pic = app.globalData.siteUrl + res.data.pic;
            }else{
              form.pic = '';
            }
            
            form.pic1 = res.data.pic;
            form.introduction = res.data.introduction;
         }
         that.setData({
           area: res.data.address
         })
         that.setData({
           form
         })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    this.category(that)
  },

  
  // 通用输入函数
  inputHandle(e) {
    let { name } = e.currentTarget.dataset;
    let value = e.detail;
    let form = this.data.form;
    form[name] = value.trim();
    this.setData({
      form
    });
  },

  // 提交
  submitHandle() {
    let {
      form
    } = this.data;
    // 表单验证
    form.ucode = app.globalData.userinfo.ucode;
    console.log(form)
    if(form.id){
      api.edit_enterprise(form).then(function(res){
        if(res.code == '0'){
          wx.showToast({
            title: '修改成功',
            success:function(){ 
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/enterprise/information/information',
                })
               }, 2000)} 
          })
        }else{
          wx.showToast({
            title: '修改失败',
            icon: 'error'
          })
        }
        
     })
    }else{
      api.add_enterprise(form).then(function(res){
        
        if(res.code == '0'){
          wx.showToast({
            title: '添加成功',
            success:function(){ 
              setTimeout(function () {
                wx.redirectTo({
                  url: '/pages/enterprise/information/information',
                })
               }, 2000)} 
          })
        }else{
          wx.showToast({
            title: '添加失败',
            icon: 'error'
          })
        }

     })
    }
  },

  afterReadLogo(event) {

    const { file } = event.detail;
    let {
      form
    } = this.data;

    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
    wx.uploadFile({
      url: app.globalData.uploadUrl, 
      filePath: file.url,
      name: 'file',
      header:header,
      formData: { user: 'test' },
      success(res) {
        var api_data = JSON.parse(res.data) 
        // 上传完成需要更新 fileList
        form.logo1 = api_data.data.pic;
      },
    });
   
    form.logo = file.url
    this.setData({
      form
    });
  },

  afterRead(event) {
    console.log(event)
    const { file } = event.detail;
    let {
      form
    } = this.data;
  
    var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' };
    wx.uploadFile({
      url: app.globalData.uploadUrl, 
      filePath: file.url,
      name: 'file',
      header:header,
      formData: { user: 'test' },
      success(res) {
        var api_data = JSON.parse(res.data) 
        // 上传完成需要更新 fileList
        form.pic1 = api_data.data.pic;
      },
    });
    form.pic = file.url
    this.setData({
      form
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  showPop1(e){
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]:true
    })
  },
  onConfirm1(event) {
    const { picker, value, index } = event.detail;
    let {form} = this.data
    form.category = value;
    this.setData({
      value02:value,
      form,
      show1:false
    })
  },
  onClose1(){
    this.setData({
      show1:false
    })
  },
   //获取分类
   category(that){
    
    api.get_category().then(function(res){
      let cate = [];
    
      for(let i = 0; i< res.data.length; i++){
         for(let j = 0; j< res.data[i].list.length; j++){
          cate.push(res.data[i].list[j].title)
         }
      }
       console.log(cate)
       that.setData({
        category: cate
       })
    })
  },

})
